package cn.laiguowei.May3.MD5Signature.utils;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;


import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import org.junit.jupiter.api.Test;

/**
* @author laiguowei E-mail:1276293854@qq.com   2021年4月30日 下午12:00:35
*/
public class AesSignatureAndMD5Utils{
	
	
	public static void main(String[] args)  {
		AesSignatureAndMD5Utils ascSignature = new AesSignatureAndMD5Utils();
		//1、生成一个对称加密的公钥  如果大家都用同一份公钥就可以正常解密。否则就会报错
		String base64key = "dDPOl95H5f7AKeJA4XgDFw==";
		//2、模拟把参数加密好
		String arg = "{\r\n"
				+ "\"count\": 696,\r\n"
				+ "\"name\": \"新能源汽车产业\",\r\n"
				+ "\"secondField\": 6112,\r\n"
				+ "\"precent\": \"100%\"\r\n"
				+ "}";
		String base64result = ascSignature.ascEncode(arg,base64key);
		try {
			String decoderStr = ascSignature.ascDecode(base64result,base64key);
			System.out.println(decoderStr);
		} catch (Exception e) {
			System.out.println("秘钥不正确请提供正确的秘钥");
		}
	}

	/**
	 * 对称加密算法加密
	 * @param content
	 * @param base64key
	 * @return
	 */
	public static String ascEncode(String content,String base64key) {
		AES aes = SecureUtil.aes(Base64.decode(base64key));
		byte[] encrypt = aes.encrypt(content);
		return Base64.encodeUrlSafe(encrypt);

	}

	/**
	 * 对称解密算法解密
	 * @param bycontent
	 * @param base64key
	 * @return
	 */
	public static String ascDecode(String bycontent,String base64key) {
		AES aes = SecureUtil.aes(Base64.decode(base64key));
		String decodeStr = new String(aes.decrypt(bycontent));
		return decodeStr;
	}
	
	/**
	 * 生成随机生成密钥
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String generateAscKey() {
		return Base64.encodeUrlSafe(SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded());
	}
	
	public String MD5ValidByTime(String asckey,TimeUnit timeUnit,Integer s) {
		if(TimeUnit.MINUTES.equals(timeUnit.SECONDS)) {
			System.out.println(1);
		}else {
			System.out.println(2);
		}
		return null;
	}
	
	public class TimeExpire{
		public final static String DAYS = "DAYS";
		public final static String HOURS = "HOURS";
		public final static String MINUTES = "MINUTES";
		public final static String SECONDS = "SECONDS";
		
		public  String type = "";
		public  Long expiretime= 0l;
		
		public  Long during = 0l;
		
		public Long getDuring() {
			return during;
		}

		public void setDuring(Long during) {
			this.during = during;
		}


		/**
		 * 构造时间
		 * @param type  秒 分 时 天
		 * @param expiretime 过期时间
		 * @return 这个返回的时候用来做时间检验的。
		 */
		public TimeExpire build(String type,Long expiretime) {
			if(DAYS.equals(type)) {
				during = expiretime*60*60*24;
			}else if(HOURS.equals(type)) {
				during = expiretime*60*60;
			}else if(MINUTES.equals(type)) {
				during = expiretime*60;
			}else if(SECONDS.equals(type)) {
				during = expiretime;
			}else {
				during = 60*expiretime;
			}
			TimeExpire time = new  TimeExpire();
			time.setDuring(during);
			return time;
		}


		/**
		 * 判读时间是否过期   过期就是true 没有过期就是false
		 * @param invokeTime
		 * @return
		 * @throws ParseException
		 */
		public boolean expire(Long invokeTime) throws ParseException {
			invokeTime = invokeTime;
			if((invokeTime+during) > System.currentTimeMillis()/1000) {
				return false;
			}else {
				return true;
			}
		}
		
		
	}
	
	@Test
	public void test() throws ParseException {
		System.out.println(new TimeExpire().build(TimeExpire.MINUTES, 5L).expire(1619763007l));
	}
	
	@Test
	public void test2() {
		AesSignatureAndMD5Utils ascSignature = new AesSignatureAndMD5Utils();
		//1、生成一个对称加密的公钥  如果大家都用同一份公钥就可以正常解密。否则就会报错
		String base64key = "dDPOl95H5f7AKeJA4XgDFw==";
		//2、模拟把参数加密好
		String arg = "{\r\n"
				+ "\"count\": 696,\r\n"
				+ "\"name\": \"新能源汽车产业\",\r\n"
				+ "\"secondField\": 6112,\r\n"
				+ "\"precent\": \"100%\"\r\n"
				+ "}";
		String base64result = ascSignature.ascEncode(arg,base64key);
		System.out.println(base64result);
	}
}
